#!/bin/bash

clear


echo SteamDeck双系统引导安装脚本
echo 进行初步的健全性检查 ...
sleep 2

# check if Bazzite or SteamOS
grep -i bazzite /etc/os-release &> /dev/null
if [ $? -eq 0 ]
then
	OS=bazzite
	EFI_PATH=/boot/efi/EFI
	BOOTX64=$EFI_PATH/BOOT/BOOTX64.EFI
	echo 脚本正在支持的操作系统上运行 - $OS.
else
	grep -i SteamOS /etc/os-release &> /dev/null
	if [ $? -eq 0 ]
	then
		OS=SteamOS
		EFI_PATH=/esp/efi
		BOOTX64=$EFI_PATH/boot/bootx64.efi
		echo 脚本正在支持的操作系统上运行 - $OS.
	else
		echo This is neither Bazzite nor SteamOS!
		echo Exiting immediately!
		exit
	fi
fi

# check if  dual boot configuration is supported
blkid | grep nvme0n1p1\: | grep Microsoft
if [ $? -eq 0 ]
then
	echo 检测到不支持的双引导配置.
	echo 确保在Windows之前安装了 $OS !
	exit
else
	echo 支持双引导配置.
fi

# define variables here
CLOVER=$(efibootmgr | grep -i Clover | colrm 9 | colrm 1 4)
REFIND=$(efibootmgr | grep -i rEFInd | colrm 9 | colrm 1 4)
ESP=$(df /dev/nvme0n1p1 --output=avail | tail -n1)
CLOVER_VERSION=5160
CLOVER_URL=https://gitee.com/leeshan520/CloverBootloader/releases/download/${CLOVER_VERSION}/Clover-${CLOVER_VERSION}-X64.iso.7z
CLOVER_ARCHIVE=$(curl -s -O -L -w "%{filename_effective}" $CLOVER_URL)
CLOVER_BASE=$(basename -s .7z $CLOVER_ARCHIVE)
CLOVER_EFI=\\EFI\\clover\\cloverx64.efi

# sanity check - is rEFInd installed?
efibootmgr | grep -i refind
if [ $? -ne 0 ]
then
	echo 未检测到rEFInd！继续安装双系统引导.
else
	echo rEFInd似乎已安装！尽最大努力卸载rEFInd!
	for rEFInd_boot in $REFIND
	do
		echo -e "$current_password\n" | sudo -S efibootmgr -b $rEFInd_boot -B &> /dev/null
	done
	echo -e "$current_password\n" | sudo -S systemctl disable bootnext-refind.service &> /dev/null
	echo -e "$current_password\n" | sudo -S rm -rf $EFI_PATH/refind &> /dev/null
	echo -e "$current_password\n" | sudo -S rm /etc/systemd/system/bootnext-refind.service &> /dev/null
	rm -rf ~/.SteamDeck_rEFInd &> /dev/null

	# check again if rEFInd is gone?	
	efibootmgr | grep -i refind
	if [ $? -ne 0 ]
	then
		echo rEFInd已成功卸载！继续安装双系统引导.
	else
		echo rEFInd仍然存在。请先手动卸载rEFInd!
		exit
	fi
fi

# obtain Clover ISO
7z x $CLOVER_ARCHIVE -aoa $CLOVER_BASE &> /dev/null
if [ $? -eq 0 ]
then
	echo 双系统引导文件已下载成功!
else
	echo 双系统引导文件下载失败!
	exit
fi

# mount Clover ISO
mkdir ~/temp-clover &> /dev/null
echo -e "$current_password\n" | sudo -S mount $CLOVER_BASE ~/temp-clover &> /dev/null
if [ $? -eq 0 ]
then
	echo 引导ISO已经安装!
else
	echo 安装ISO时出错!
	echo -e "$current_password\n" | sudo -S umount ~/temp-clover
	rmdir ~/temp-clover
	exit
fi

# copy Clover files to EFI system partition
echo -e "$current_password\n" | sudo -S cp -Rf ~/temp-clover/efi/clover $EFI_PATH
echo -e "$current_password\n" | sudo -S cp custom/config.plist $EFI_PATH/clover/config.plist
echo -e "$current_password\n" | sudo -S cp -Rf custom/themes/* $EFI_PATH/clover/themes

# delete temp directories created and delete the Clover ISO
echo -e "$current_password\n" | sudo -S umount ~/temp-clover
rmdir ~/temp-clover
rm Clover-$CLOVER_VERSION-X64.iso*

# remove previous Clover entries before re-creating them
for entry in $CLOVER
do
	echo -e "$current_password\n" | sudo -S efibootmgr -b $entry -B &> /dev/null
done

# install Clover to the EFI system partition
echo -e "$current_password\n" | sudo -S efibootmgr -c -d /dev/nvme0n1 -p 1 -L "Clover - GUI Boot Manager" -l "$CLOVER_EFI" &> /dev/null

# check if bootx64.efi.orig already exists
echo -e "$current_password\n" | sudo -S test -e $BOOTX64.orig
if [ $? -eq 0 ]
then
	echo $BOOTX64.orig 已找到-无需任何操作.
else
	echo $BOOTX64 备份没有被发现.
	echo -e "$current_password\n" | sudo -S cp $BOOTX64 $BOOTX64.orig
	echo -e "$current_password\n" | sudo -S cp $EFI_PATH/clover/cloverx64.efi $BOOTX64
	echo 复制 Clover EFI 到 $BOOTX64 - 完成.
fi

# check if Windows EFI needs to be disabled!
echo -e "$current_password\n" | sudo -S test -e $EFI_PATH/Microsoft/Boot/bootmgfw.efi.orig
if [ $? -eq 0 ]
then
	echo Windows EFI备份已存在。检查是否需要禁用Windows EFI.
	echo -e "$current_password\n" | sudo -S test -e $EFI_PATH/Microsoft/Boot/bootmgfw.efi
	if [ $? -eq 0 ]
	then
		echo -e "$current_password\n" | sudo -S mv $EFI_PATH/Microsoft/Boot/bootmgfw.efi $EFI_PATH/Microsoft/bootmgfw.efi &> /dev/null
		echo 需要禁用Windows EFI-已完成.
	else
		echo Windows EFI已禁用-无需任何操作.
	fi
else
	echo Windows EFI备份不存在.
	echo -e "$current_password\n" | sudo -S cp $EFI_PATH/Microsoft/Boot/bootmgfw.efi $EFI_PATH/Microsoft/Boot/bootmgfw.efi.orig &> /dev/null
	echo -e "$current_password\n" | sudo -S mv $EFI_PATH/Microsoft/Boot/bootmgfw.efi $EFI_PATH/Microsoft/bootmgfw.efi &> /dev/null
	echo 需要禁用Windows EFI-已完成.
fi

# re-arrange the boot order and make Clover the priority!
echo -e "$current_password\n" | sudo -S efibootmgr -n $CLOVER &> /dev/null
echo -e "$current_password\n" | sudo -S efibootmgr -o $CLOVER &> /dev/null

# Final sanity check
efibootmgr | grep "Clover - GUI" &> /dev/null
if [ $? -eq 0 ]
then
	echo 双系统引导已成功安装到EFI系统分区!
else
	echo 出现未知错误. 双系统引导未安装.
	exit
fi

# create ~/1Clover-tools and place the scripts in there
mkdir ~/1Clover-tools &> /dev/null
rm -f ~/1Clover-tools/* &> /dev/null
cp custom/Clover-Toolbox.sh ~/1Clover-tools &> /dev/null
echo -e "$current_password\n" | sudo -S cp custom/clover-bootmanager.service custom/clover-bootmanager.sh /etc/systemd/system
cp -R custom/logos ~/1Clover-tools &> /dev/null
cp -R custom/efi ~/1Clover-tools &> /dev/null

# make the scripts executable
chmod +x ~/1Clover-tools/Clover-Toolbox.sh
echo -e "$current_password\n" | sudo -S chmod +x /etc/systemd/system/clover-bootmanager.sh

# start the clover-bootmanager.service
echo -e "$current_password\n" | sudo -S systemctl daemon-reload
echo -e "$current_password\n" | sudo -S systemctl enable --now clover-bootmanager.service
echo -e "$current_password\n" | sudo -S /etc/systemd/system/clover-bootmanager.sh

# custom config if using SteamOS or Bazzite
if [ $OS = SteamOS ]
then
	echo $OS 进行最终配置.
	mkdir -p ~/.local/share/kservices5/ServiceMenus
	cp custom/open_as_root.desktop ~/.local/share/kservices5/ServiceMenus
	echo -e "$current_password\n" | sudo -S cp custom/clover-whitelist.conf /etc/atomic-update.conf.d
else
	echo Making final configuration for $OS.
	echo -e "$current_password\n" | sudo -S blkid | grep nvme0n1p1 | grep esp &> /dev/null
	if [ $? -eq 0 ]
	then
		echo ESP partition is already labeled - no action needed.
	else
		echo -e "$current_password\n" | sudo -S fatlabel /dev/nvme0n1p1 esp &> /dev/null
		echo ESP partition label has been completed.
	fi

	# set bazzite as the default boot in Clover config
	echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultLoader<\/key>/!b;n;c\\t\t<string>\\EFI\\FEDORA\\SHIMX64\.efi<\/string>' $EFI_PATH/clover/config.plist

fi

# create desktop icon for Clover Toolbox
ln -s ~/1Clover-tools/Clover-Toolbox.sh ~/Desktop/双系统引导配置工具 &> /dev/null
echo -e 工具箱桌面图标已创建!



echo 双系统引导安装成功!
